﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Intorise.Net;
using System.Net;
using System.Threading;

namespace TestWindowsFormsApplication
{
    public partial class ValidateCodeFrom : Form
    {
        NetTools tools = new NetTools();
        CookieContainer container = new CookieContainer();
        Uri uri = new Uri("http://www.17track.net");
        static string validateCodeUrl = "http://www.17track.net/f/firewallcaptchahandler.ashx?get=validationresult&c={0}&t={1}&i={2}&AspxAutoDetectCookieSupport=1";
        public ValidateCodeFrom()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
        }

        public DialogResult ShowValidateCode()
        {
            DialogResult = DialogResult.No;
            LoadCode();
            System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(x =>
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }), null,  60*1000, Timeout.Infinite);
            base.ShowDialog();
            return this.DialogResult;
        }

        private void LoadCode()
        {
            this.pictureBox1.Image = null;
            int count = 0;
            var request = tools.SendRequest("http://www.17track.net/f/firewallcaptchahandler.ashx?get=image", "GET", container);
            var response = (HttpWebResponse)request.GetResponse();
            while (response.StatusCode == HttpStatusCode.Redirect && count < 5)
            {
                count++;
                response.Close();
                var queryUrl = "http://www.17track.net" + response.Headers[HttpResponseHeader.Location];
                request = tools.SendRequest(queryUrl, "GET", container);
                response = (HttpWebResponse)request.GetResponse();
            }
            var image = Image.FromStream(response.GetResponseStream());
            response.Close();
            this.pictureBox1.Image = image;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "")
            {
                return;
            }
            HttpWebResponse response = null;
            this.button1.Enabled = false;

            var c = container.GetCookies(uri)["captchaId"].Value;
            var t = container.GetCookies(uri)["instanceId"].Value;
            try
            {
                var request = tools.SendRequest(string.Format(validateCodeUrl, c, t, this.textBox1.Text), "GET", container);
                response = (HttpWebResponse)request.GetResponse();
                var result = tools.GetResponseHtmlGzip(response);
                if (result == "true")
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.label1.Visible = true;
                    LoadCode();
                    this.label1.Text = "";
                    this.label1.Focus();
                }
            }
            finally
            {
                response.Close();
                this.button1.Enabled = true;
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(null, null);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            LoadCode();
        }
    }
}
